package cn.ai.shoes.core.handler;


import cn.ai.shoes.common.pojo.entity.CommonResult;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import java.io.IOException;

public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        // 调试输出当前用户信息
        if (authentication != null) {
            System.out.println("[LogoutSuccessHandler] 当前登出用户: " + authentication.getPrincipal());
        } else {
            System.out.println("[LogoutSuccessHandler] 当前登出用户: null");
        }
        //将result象转换为json字符串
        String json= JSON.toJSONString(CommonResult.success());
        //设置响应头
        response.setContentType("application/json;charset=utf-8");
        //设置响应体
        response.getWriter().print(json);
    }
}
